##Alison E. Post##
##"Foreign and Domestic Investment in Argentina" (2014)##
##Replication Code for Time Series Arbitration Analysis, Table 7.7###

library(foreign)
library(survival)
library(Zelig)
library(car)

##Note: all analyses for the book were originally conducted using the R package "Zelig".  After the proofs for the book were submitted, Zelig stopped supporting the Cox proportional hazard model. I have therefore added R survival package commands to obtain key table results throughout this file. For this particular table, the R/S+ commands yield slightly different coefficient estimates than the Zelig commands did.  Results are substantively similar, however. ## 

##Load data and create relevant subset##
foreign_dom <- read.csv("replication.timeseries.csv", header=TRUE) 

##Create subset of data with just concessions and divestitures##
div.con <- foreign_dom[foreign_dom$Type_PPI!="Greenfield Project",]
div.con <- div.con[div.con$Type_PPI!="Management, Lease Contract",] 
foreign.div.con<- div.con[div.con$X25_Foreign==1,]
			

##Model 1: Baseline Model##

attach(foreign.div.con)
div.con.subset <- data.frame(Project_name, Lifespan, Cancelled_In_Year, start, stop, BIT_Arb, Arb_Strength, Alt_Treaty_Arb, Dom_I_Law_Arb, GDPpc1991, WB_Rule_Law, Polity, Country,Type_PPI, Region)
div.con.subset <- na.omit(div.con.subset)
detach(foreign.div.con)

	#Create Strong Version of the Arbitration Variable#
div.con.subset$Arbitration <- 0

for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]=="Comprehensive" | div.con.subset$Alt_Treaty_Arb[i]=="Comprehensive") 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}

z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(start, stop, Cancelled_In_Year) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + strata(Type_PPI) + cluster(Project_name), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)
	##Note that results using S+ syntax are slightly different than those reported by Zelig (presented in book), though substantively 		similar##

##Model 2: First adding time-varying covariates##

attach(foreign.div.con) ##creating subset of data that includes Civil Liberties and other Time-Varying Covariates##
div.con.subset <- data.frame(Project_name, Lifespan, Cancelled_In_Year, start, stop, BIT_Arb, Arb_Strength, Alt_Treaty_Arb, Dom_I_Law_Arb, GDPpc1991, WB_Rule_Law, Polity, Country,Type_PPI, Region, FH_CL, Leftgov, After.Crisis, Checks, Multi_Part)
div.con.subset <- na.omit(div.con.subset)
detach(foreign.div.con)

	#Create Strong Version of the Arbitration Variable#
div.con.subset$Arbitration <- 0

for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]=="Comprehensive" | div.con.subset$Alt_Treaty_Arb[i]=="Comprehensive") 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}

z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + FH_CL + Leftgov + Checks + Leftgov*Checks + After.Crisis + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(start, stop, Cancelled_In_Year) ~ Arbitration + GDPpc1991 + FH_CL + Leftgov + Checks + Leftgov*Checks + After.Crisis + strata(Type_PPI) + cluster(Country) + cluster(Project_name), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)


##Model 3: Adding an interaction between strong arbitration and crisis
z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + FH_CL + Leftgov + Checks + Leftgov*Checks + After.Crisis + After.Crisis*Arbitration + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(start, stop, Cancelled_In_Year) ~ Arbitration + GDPpc1991 + FH_CL + Leftgov + Checks + Leftgov*Checks + After.Crisis + After.Crisis*Arbitration + strata(Type_PPI) + cluster(Country) + cluster(Project_name), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)


##Model 4: Examining the Influence of Multilateral Participation
z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + FH_CL + Leftgov + Checks + Leftgov*Checks + After.Crisis + Multi_Part + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(start, stop, Cancelled_In_Year) ~ Arbitration + GDPpc1991 + FH_CL + Leftgov + Checks + Leftgov*Checks + After.Crisis + Multi_Part  + strata(Type_PPI) + cluster(Country) + cluster(Project_name), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)

